package com.fs.jzzn.pojo;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Table;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Formula;

import com.fs.base.param.SysConstant;
import com.fs.base.pojo.PEntity;
import com.fs.base.utils.ExcelUtils;

/**
 * @author TiddlerCJ
 * @desc 工程收入
 */
@Entity
@Table(name="p_income")
@Data@EqualsAndHashCode(callSuper=false)
public class Income extends PEntity {
	private String incomeNo;		//收入编号
	private String pNo;				//关联对应单项工程编号--->修改成订单编号
	
	@Formula("( SELECT p.contract_title FROM p_contract p WHERE p.contract_no=p_no and p.contract_no <> '' limit 1 )")
	private String pContractTitle;//订单或合同名称
	
	//private String status;		//回款状态
	private Double money;			//回款金额
	private String returnedDate;	//回款日期
	private String verifyUserName;	//回款确认人
	private String billsNo;			//凭单号
	private String companyName;//对方户名
	
	private String incomeType;	//回款类型 预付款-进度款-尾款-全款
	private String incomeRate;	//回款比例
	//private String department;	//关联项目部
	
	public static List<Income> excelDataFormat(List<List<String>> data) {
		List<Income> list = new ArrayList<Income>();
		for(List<String> rowData: data){
			list.add(excelData2Bean(rowData));
		}
		return list;
	}
	
	//[\"项目编号\",\"回款金额\",\"回款日期\",\"回款确认人\",\"对方户名\",\"凭单号\",\"备注\"]"
	private static Income excelData2Bean(List<String> row) {
		row = ExcelUtils.dataLenFomart(row, 13);
				
		DecimalFormat df  = new DecimalFormat(".00");
		Income in = new Income();
		in.setIncomeNo(row.get(0));//单据编号
		in.setPNo(row.get(1));//订单编号及名称
		try {
			Double money = Double.parseDouble(row.get(3));
			in.setMoney( Double.parseDouble(df.format(money)));//回款金额(元)
		} catch (Exception e) {
			System.err.println("回款金额格式有误："+row.get(3) );
		}
		in.setIncomeRate(row.get(4));//回款比例
		System.out.println(1);
		in.setIncomeType(row.get(5));//回款类型
		System.out.println(2);
		in.setReturnedDate(row.get(6));//回款日期
		System.out.println(3);
		System.out.println(31);
		System.out.println(row.get(7));
		in.setVerifyUserName(row.get(7));//回款确认人
		System.out.println(4);
		in.setCompanyName(row.get(8));//对方户名
		System.out.println(5);
		in.setBillsNo(row.get(9));//凭单号
		System.out.println(6);
		in.setOrgNo(row.get(10));//所属机构
		System.out.println(7);
		in.setRemark(row.get(11));//备注
		System.out.println(12);
		in.setCreateTime(new Date());//创建时间
		System.out.println(4);
		String userName = row.get(12);
		System.out.println(5);
		if(StringUtils.isNotBlank(userName)){
			String userId = SysConstant.getKey(SysConstant.USERS_ID_NAME_MAP, userName);
			in.setCreaterId(userId);
		}//创建人
		return in;
	}
}
